{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8ca5f36",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import yfinance as yf\n",
    "from utils import plot_returns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62c400f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download front-month futures data of S&P500, 10-year Treasuries, gold and US dollar\n",
    "symbols = ['ES=F', 'ZN=F', 'GC=F', 'DX=F']\n",
    "data = yf.download(symbols)\n",
    "# Resample data so that we deal with monthly data instead of daily to reduce noise\n",
    "data = data.resample(\"M\").last()\n",
    "data.index = pd.to_datetime(data.index)\n",
    "# Subset adjusted close prices and fill NaNs with value know at time t\n",
    "# Drop rows with unknown prices in the beginning of the dataset\n",
    "prices = data[\"Adj Close\"].ffill().dropna()\n",
    "prices.index = pd.to_datetime(prices.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12b83be2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute logarithmic returns\n",
    "log_returns = np.log(prices).diff()\n",
    "# Compute risk parity weights\n",
    "annual_vol = None\n",
    "inverse_vol = None\n",
    "risk_parity_weights = None\n",
    "weighted_returns = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43566d5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Annualize returns and volatility\n",
    "annualized_return = None\n",
    "annualized_vol = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6332f5ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate the Sharpe Ratio\n",
    "sharpe_ratio = None\n",
    "# Calculate the Sortino Ratio\n",
    "downside_vol = None\n",
    "sortino_ratio = None\n",
    "# Calculate the Calmar Ratio\n",
    "cum_returns = None\n",
    "drawdowns = None\n",
    "max_drawdown = None\n",
    "calmar_ratio = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b9a3b3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "print()\n",
    "print(f\"annualized_return: {np.round(annualized_return * 100, 1)}%\")\n",
    "print(f\"annualized_volatility: {np.round(annualized_vol * 100, 1)}%\")\n",
    "print(f\"downside_volatility: {np.round(downside_vol * 100, 1)}%\")\n",
    "print(f\"max_drawdown: {np.round(max_drawdown * 100, 1)}%\")\n",
    "print()\n",
    "print(f\"sharpe_ratio: {np.round(sharpe_ratio, 2)}\")\n",
    "print(f\"sortino_ratio: {np.round(sortino_ratio, 2)}\")\n",
    "print(f\"calmar_ratio: {np.round(calmar_ratio, 2)}\")\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8243d58c",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_returns(weighted_returns)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
